<?php

class AcademyController extends Core_Controller_Action 
{
	public $menuType = 3;
	
	public $newsLimit = 12;
	
	private $_news;
	
	public function init() {
		$this->view->menuType = $this->menuType;
		$this->_news = new News();
	}
	
	public function indexAction() {
		$this->_news = $this->_news->getItemService(16)->getFirst();
		
		if($this->_news)
		{
			$this->_news = $this->_news->toArray();
			
			$this->view->item = $this->_news;
		}
		else
		{
			$this->view->empty = true;
		}
		
	}
	
	public function showAction() {
		$id = intval( $this->_request->getParam( 'id', 0 ) );
		$page = intval( $this->_request->getParam( 'page', 1 ) );
		$parent = intval( $this->_request->getParam( 'p', 0 ) );
		$type = 3;

		
		if( $id > 0 ) {			
			$this->view->id = $id;
			$this->view->parent = $parent;
	
			$pager = $this->_news->getListByCategoryWithPagerService( $id, $this->view->baseUrl(). '/academy/show/page/{%page_number}/id/'.$id.'/p/'.$parent, $page, $this->newsLimit );
			
			if( $pager ) {
				$pagerLayout = $pager['layout'];
				$pagerLayout->setTemplate('<a class="nums" href="{%url}">{%page}</a>');
				$pagerLayout->setSelectedTemplate('<a class="nums focus" href="{%url}">{%page}</a>');
				
				$pager = $pager['pager'];
		
				$this->view->list = $pagerLayout->execute();
				$this->view->pager = $pagerLayout;
				
				$this->view->result = $pager->getNumResults();
				$this->view->page = $page;
				$this->view->pathInfo = $this->view->baseUrl() . $this->_request->getPathInfo();
				$this->view->limit = $this->newsLimit;
			
			} else {
				$this->view->emptyResult = true;
			}
		} else {
			$this->_forward( 'p404', 'index' );
		}
	}
	
	public function articleAction() {
		$id = intval( $this->_request->getParam( 'id', false ) );

		if( $id ) {
			$item = $this->_news->getItemService( $id );
			if( $item ) {				
				$this->view->item = $item->getFirst()->toArray();
			} else {
				$this->_forward( 'p404', 'index' );
			}
		} else {
			$this->_forward( 'p404', 'index' );
		}
	}
}